package com.cch.examSystem.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class AlterManagerInfoServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String method = request.getParameter("method");
		JSONObject jo = new JSONObject();
		switch (method) {
		case "scaleList":
			System.out.println("scaleList");
			getValue(request, response);
			break;
		case "studentList":
			System.out.println("studentList");
			getValue(request, response);
			break;
		case "getSubjectSelect":
			System.out.println("getSubjectSelect");
			String major = request.getParameter("major");
			if (major.equalsIgnoreCase("IT")) {
				String[] subjects = { "C", "python", "java", "scala" };
				jo.put("subject", subjects);
			} else if (major.equalsIgnoreCase("数学")) {
				String[] subject = { "高数", "数学与应用数学", "信息与计算科学" };
				jo.put("subject", subject);
			}
			break;
		case "getKnowledgeSelect":
			System.out.println("getKnowledgeSelect");
			String subject = request.getParameter("subject");
			if (subject.equalsIgnoreCase("java")) {
				String[] knowledges = { "知识点0", "知识点1", "知识点2", "知识点3" };
				jo.put("knowledge", knowledges);
			} else if (subject.equalsIgnoreCase("c")) {
				String[] knowledges = { "知识点4", "知识点5", "知识点6" };
				jo.put("knowledge", knowledges);
			} else {
				String[] knowledges = { "知识点7", "知识点8", "知识点9" };
				jo.put("knowledge", knowledges);
			}
			break;
		case "getMajorSelect":
			System.out.println("getMajorSelect");
			String[] majors = { "IT", "数学"};
			jo.put("major", majors);

			break;
		}
		jo.put("code", 200);
		System.out.println(jo.toJSONString());
		out.print(jo.toJSONString());
		out.flush();
		out.close();
	}

	public static void getValue(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, String[]> map = request.getParameterMap();
		for (String key : map.keySet()) {
			System.out.println(key + " : " + map.get(key)[0]);
		}
	}

}
